<?php
if (class_exists('PDO')) {
  $drivers = PDO::getAvailableDrivers();
  if(in_array('cubrid', $drivers)) {
  } else {
    echo "Error: PDO is not detected!" . "<br />";
    echo "Please check your instalation (PHP PDO)." . "<br />";      
    exit;
  }
} else {
  echo "Error: CUBRID PDO Driver is not detected!" . "<br />";
  echo "Please check your instalation (CUBRID PDO Driver)." . "<br />";      
  exit;
}

class ConnectionInfo {
  public $conn; //PDO connection handlle

  //connection parameters
  private $dbname;
  private $dbuser;
  private $dbpassword;
  private $dbport;
  private $dbhost;
  
  private $err_msg; //error message
  
  function __construct($dbname = "", $dbuser = "", $dbpassword = "", $dbhost = "", $dbport = "") {
    $this->dbname = $dbname;
    $this->dbuser = $dbuser;
    $this->dbpassword = $dbpassword;
    $this->dbhost = $dbhost;
    $this->dbport = $dbport;
  } 

  // this will be called automatically at the end of scope
  public function __destruct() {
    $this->conn = null; //force disconnect
  }
   
  //database name
  public function setDbName( $value ) {
    $this->dbname = $value;
  }
  public function getDbName() {
    return $this->dbname;
  }

  //user name
  public function setDbUser( $value ) {
    $this->dbuser = $value;
  }
  public function getDbUser() {
    return $this->dbuser;
  }

  //user password
  public function setDbPassword( $value ) {
    $this->dbpassword = $value;
  }
  public function getDbPassword() {
    return $this->dbpassword;
  }

  //database host/server port
  public function setDbPort( $value ) {
    $this->dbport = $value;
  }
  public function getDbPort() {
    return $this->dbport;
  }

  //database host/server address
  public function setDbHost( $value ) {
    $this->dbhost = $value;
  }
  public function getDbHost() {
    return $this->dbhost;
  }

  //error message
  public function setDbErrorMsg( $value ) {
    $this->err_msg = $value;
  }
  public function getDbErrorMsg() {
    return $this->err_msg;
  }

  public function connect($forced = false) {
    try {
      if($this->conn && !$forced) {
        return true; //reuse existing connection
      }
      if($this->conn && $forced) {
        $this->conn = null; //force disconnect
      }
      
      if($this->getDbName() == "" || $this->getDbHost() == "") {
        $this->conn = null;
        $this->setDbErrorMsg("Connection parameters are not set!");
        return false;
      }

      $conn_str = "cubrid:dbname=".$this->getDbName().";host=".$this->getDbHost().";port=".$this->getDbPort();
      $dbconn = new PDO($conn_str, $this->getDbUser(), $this->getDbPassword());
      
      if($dbconn) {
        $this->conn = $dbconn;
        return true;
      } else {
        $this->conn = null;
        $this->setDbErrorMsg("Could not connect to the database!");
        return false;
      }
      return $conn;
    } catch(PDOException $e) {
      $this->conn = null;
      $this->setDbErrorMsg($e);
      return false;
    }
  }

  private function getAttributeValue($value) {
    if($this->conn) {
      return $this->conn->getAttribute($value);
    } else {
      return "";
    }
  }
     
  //CUBRID server version
  public function server_db_version() {
    return $this->getAttributeValue(PDO::ATTR_SERVER_VERSION);
  }  
  
  //CUBRID server information
  public function server_info() {
    return $this->getAttributeValue(PDO::ATTR_SERVER_INFO);
  }  
  
  //CUBRID client version
  public function client_db_version() {
    return $this->getAttributeValue(PDO::ATTR_CLIENT_VERSION);
  }  

  //CUBRID autocommit setting
  public function autocommit() {
    return $this->getAttributeValue(PDO::ATTR_AUTOCOMMIT);
  }  

  //CUBRID driver name
  public function driver_name() {
    return $this->getAttributeValue(PDO::ATTR_DRIVER_NAME);
  }  

  //CUBRID ORACLE NULLs: Conversion of NULL and empty strings
  public function ORACLE_NULLs() {
    return $this->getAttributeValue(PDO::ATTR_ORACLE_NULLS);
  }  

  //CHARSET
  public function Charset() {
    $ret = "";

    $sql = "select charset from db_root";
    $result = $this->conn->query($sql);
    if($result != null) {
      foreach ($result as $row) {
        switch ($row[0]) {
          case "0":
            $ret = "US English charset - ASCII encoding";
            break;
          case "1":
            $ret = "Uninterpreted bits - Raw encoding";
            break;
          case "2":
            $ret = "Uninterpreted bytes - Raw encoding";
            break;
          case "3":
            $ret = "Latin 1 charset - ISO 8859 encoding";
            break;
          case "4":
            $ret = "KSC 5601 1990 charset - EUC encoding";
            break;
          default:
            $ret = "Unknown";
        }
      }
    }
    return $ret;
  }  

}  

?>
